package com.kelaite.procurement.service.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kelaite.procurement.entity.TOrderHistory;
import com.kelaite.procurement.repository.OrderHistoryRepository;
import com.kelaite.procurement.service.OrderHistoryService;

@Service
@Transactional
public class OrderHistoryServiceImpl implements OrderHistoryService{
  
	@Autowired
	private OrderHistoryRepository orderHistoryRepository; 
	
	@PersistenceContext
	private EntityManager em;
	
	@Override
	public TOrderHistory save(TOrderHistory persistence) {
		return this.orderHistoryRepository.save(persistence);
	}

	@Override
	public TOrderHistory getObject(Long id) {
		return this.orderHistoryRepository.findOne(id);
	}

	@Override
	public List<TOrderHistory> getAll() {
		return this.orderHistoryRepository.findAll();
	}

	@Override
	public void deleteByObject(TOrderHistory persistence) {
		this.orderHistoryRepository.delete(persistence);
	}

	@Override
	public void deleteByPrimaryKey(Long id) {
		this.orderHistoryRepository.delete(id);
	}
	
	@SuppressWarnings({ "deprecation", "unchecked" })
	@Override
	public List<TOrderHistory> getByProperty(String property, Object value) {
		Session session = (org.hibernate.Session) em.getDelegate();
		Criteria criteria = session.createCriteria(TOrderHistory.class).add(org.hibernate.criterion.Expression.eq(property, value));
		return criteria.list();
	}
	
}
